    //
//  VmixGridViewCell.m
//  VMIX
//
//  Created by jeffs on 6/1/10.
//  Copyright 2010 Vmix Media, INC. All rights reserved.
//

#import "VmixGridViewCell.h"

@implementation VmixGridViewCell

@synthesize imageCache,imagePath, imageView, title, description;

- (id) initWithFrame: (CGRect) frame reuseIdentifier: (NSString *) aReuseIdentifier
{
    self = [super initWithFrame: frame reuseIdentifier: aReuseIdentifier];
    if ( self == nil )
        return ( nil );
    imagePath = @"";
    imageView = [[UIImageView alloc] initWithFrame: CGRectZero];
    title = [[UILabel alloc] initWithFrame: CGRectZero];
	description = [[UILabel alloc] initWithFrame: CGRectZero];
	description.numberOfLines = 3;
	description.highlightedTextColor =[UIColor grayColor];
	description.textColor = [UIColor grayColor];
	description.font = [UIFont boldSystemFontOfSize: 12.0];
	description.minimumFontSize = 12.0;
	description.adjustsFontSizeToFitWidth = YES;
	description.backgroundColor = [UIColor clearColor];
    title.highlightedTextColor = [UIColor whiteColor];
	title.textColor = [UIColor whiteColor];
    title.font = [UIFont boldSystemFontOfSize: 14.0];
    title.adjustsFontSizeToFitWidth = YES;
    title.minimumFontSize = 14.0;
	title.backgroundColor = [UIColor clearColor];
	title.textAlignment = UITextAlignmentLeft;
    
    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = self.backgroundColor;
    imageView.backgroundColor = self.backgroundColor;
    
    [self.contentView addSubview: imageView];
    [self.contentView addSubview: title];
	[self.contentView addSubview: description];
    imageCache = [[NSMutableDictionary alloc] init];
    return ( self );
}

- (void) dealloc
{
    [imageView release];
    [title release];
    [super dealloc];
}


- (void) layoutSubviews
{
    [super layoutSubviews];

    CGSize imageSize = CGSizeMake(224,145);
    CGRect bounds = CGRectInset( self.contentView.bounds, 0.0, 0.0 );
	
	[description sizeToFit];
    CGRect frame = description.frame;
    frame.size.width = 190;
	frame.size.height = 65;
    frame.origin.y = CGRectGetMaxY(bounds) - frame.size.height;
    frame.origin.x = floorf((bounds.size.width - frame.size.width) * 0.5);
    description.frame = frame;
    
    [title sizeToFit];
    frame = title.frame;
    frame.size.width = 190;
    frame.origin.y = CGRectGetMaxY(bounds) - frame.size.height - 60;
    frame.origin.x = floorf((bounds.size.width - frame.size.width) * 0.5);
    title.frame = frame;

    
    // adjust the frame down for the image layout calculation
    bounds.size.height = frame.origin.y - bounds.origin.y;
    
    if ( (imageSize.width <= bounds.size.width) &&
        (imageSize.height <= bounds.size.height) )
    {
        return;
    }
    
    // scale it down to fit
    // CGFloat hRatio = bounds.size.width / imageSize.width;
    // CGFloat vRatio = bounds.size.height / imageSize.height;

    [imageView sizeToFit];
    
	 frame = imageView.frame;
	/*
    frame.size.width = floorf(imageSize.width * ratio);
    frame.size.height = floorf(imageSize.height * ratio);
    frame.origin.x = floorf((bounds.size.width - frame.size.width) * 0.5);
    frame.origin.y = floorf(((bounds.size.height - frame.size.height) * 0.5) + 5);
	 */
	frame.size.width = 190;
	frame.size.height = 140;
	frame.origin.x = floorf(bounds.size.width - frame.size.width - 5);
	frame.origin.y = floorf(bounds.size.height - frame.size.height - 5);
    imageView.frame = frame;
 
}

@end
